﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data.Objects;
using System.ComponentModel;

namespace RL
{
    /// <summary>
    /// Логика взаимодействия для wRouteType.xaml
    /// </summary>
    public partial class wRouteType : Window
    {
        private routelistEntities db;
        private IEditableCollectionView gView;
        public wRouteType()
        {
            InitializeComponent();
            db = new routelistEntities();
        }

        private void buttonAdd_Click(object sender, RoutedEventArgs e)
        {
            wRouteTypeDialog dlg = new wRouteTypeDialog();
            //RouteType rt = new RouteType { del = false, name = "<введите тип маршрута>" };
            RouteType rt = (RouteType)gView.AddNew();
            rt.del = false;
            rt.name = "<введите тип маршрута>";
            dlg.DataContext = rt;
            if (dlg.ShowDialog() == true)
            {
                db.RouteType.AddObject(rt);
                Save();
                Refresh(rt);
            }
            else
            {
            }
        }

        private void buttonDelete_Click(object sender, RoutedEventArgs e)
        {
            RouteType item = listView1.SelectedItem as RouteType;
            if (null == item) return;
            item.del = !item.del;
            Save();
        }

        private void buttonEdit_Click(object sender, RoutedEventArgs e)
        {
            ObjectStateEntry entry;
            RouteType item = listView1.SelectedItem as RouteType;
            if (null == item) return;
            wRouteTypeDialog dlg = new wRouteTypeDialog();
            dlg.DataContext = item;
            if (dlg.ShowDialog() == true)
            {
                Save();
                Refresh(item);
            }
            else
            {
                db.ObjectStateManager.TryGetObjectStateEntry(item, out entry);
                if (entry.State == System.Data.EntityState.Modified)
                {
                    db.RouteType.Context.Refresh(RefreshMode.StoreWins, item);
                }
            }
        }

        private void Save()
        {
            try
            {
                db.SaveChanges();
                //MessageBox.Show("OK");
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            var query = from o in db.RouteType orderby o.name select o;
            CollectionViewSource gViewSource = (CollectionViewSource)FindResource("sourceRouteType");
            gViewSource.Source = query;
            gView = (IEditableCollectionView)gViewSource.View;
        }

        private void Refresh(RouteType item)
        {
            RouteType curItem = item;
            IQueryable<RouteType> query = from o in db.RouteType orderby o.name select o;
            CollectionViewSource src = (CollectionViewSource)FindResource("sourceRouteType");
            src.Source = query;
            gView = (IEditableCollectionView)src.View;
            listView1.ScrollIntoView(curItem);
            listView1.SelectedItem = curItem;
        }
    }
}
